﻿Imports System.Data.Sql
Imports System.Data.SqlClient


Public Class Frm_ParameterSet

    Implements IDServer.IDServer.IDServer


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private WithEvents MainForm As Frm_MainForm
    Private bFind As Boolean


    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

#Region "interface define"


    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        Try
            If AddEnable = False Then
                MessageBox.Show("對不起，您沒有新增權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            Me.TabControl1.SelectedIndex = 1
            ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
            ClearControl()
            SetControlEnable(False, True)
            MainForm.BtnEnable(False)
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            ClearControl()
            SetControlEnable(False, False)
            If Me.TabControl1.SelectedIndex = 1 Then
                If Me.DataGridView1.Rows.Count > 0 Then
                    Dim fCode As String = Me.DataGridView1.CurrentRow.Cells("fCode").Value
                    FillText(fCode)
                End If
            End If
            MainForm.BtnEnable(True)
            GetToolEnable()
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Try
            If DeleteEnable = False Then
                MessageBox.Show("對不起，您沒有刪除權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If


            Dim fCode As String = Me.DataGridView1.CurrentRow.Cells("fCode").Value
            If MsgBox(My.resources.ConfirmDelete, MsgBoxStyle.Question + MsgBoxStyle.OkCancel, Msg.CompanyName) = MsgBoxResult.Ok Then
                If DeleteDataset(fCode) = True Then

                    MsgBox(My.Resources.DeleteSuccess, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)

                    If Me.TabControl1.SelectedIndex = 1 Then
                        If Me.DataGridView1.Rows.Count > 0 Then
                            FillText(Me.DataGridView1.CurrentRow.Cells("fCode").Value)
                        End If
                    End If
                End If
            End If

        Catch ex As Exception
            MsgBox("ActionDelete:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        Try
            If EditEnable = False Then
                MessageBox.Show("對不起，您沒有修改權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            Me.TabControl1.SelectedIndex = 1
            ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
            SetControlEnable(False, True)
            Me.txtCode.Enabled = False
            MainForm.BtnEnable(False)
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Private Sub ActionFind()
       
    End Sub

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

        Dim sql As String = ""
        Dim mTable As DataTable



        If Me.txtCode.Text.Length <= 0 Then
            MsgBox(Msg.NotNullfCode, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Me.txtCode.Focus()
            Return False
        End If

        If Me.cmbStore.SelectedIndex = -1 Then
            MsgBox("請選擇要設定的倉庫！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Me.cmbStore.Focus()
            SendKeys.Send("{F4}")
            Return False
        End If

        If Me.txtTon.Text.Length <= 0 Then
            MsgBox("數據不能為空!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Me.txtTon.Focus()
            Return False
        End If

        If Me.txtSqure.Text.Length <= 0 Then
            MsgBox("數據不能為空!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Me.txtSqure.Focus()
            Return False
        End If

        If Me.txtMoney.Text.Length <= 0 Then
            MsgBox("數據不能為空!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Me.txtMoney.Focus()
            Return False
        End If

        If CDate(Me.DateTimePicker1.Value) > CDate(Me.DateTimePicker2.Value) Then
            MsgBox(My.Resources.NotLargeEndDate, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Me.DateTimePicker1.Focus()
            SendKeys.Send("{F2}")
            Return False
        Else
            sql = "select StartDate,EndDate from Logic_ParameterSetA where StoreCode='" & Me.cmbStore.SelectedValue.ToString & "'"
            mTable = New DataTable
            Try
                mTable = ReturnDataTable(sql)

                If mTable.Rows.Count > 0 Then
                    For Each mRow As DataRow In mTable.Rows

                        If CDate(FormatDate(Me.DateTimePicker1.Value)) >= CDate(mRow("StartDate")) And CDate(FormatDate(Me.DateTimePicker1.Value)) <= CDate(mRow("EndDate")) Then
                            MsgBox("參數的有效日期存在交叉,請重新選定日期！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                            Me.DateTimePicker1.Focus()
                            SendKeys.Send("{F4}")
                            Return False
                            Exit Function
                        ElseIf CDate(FormatDate(Me.DateTimePicker2.Value)) <= CDate(mRow("StartDate")) Then
                            MsgBox("參數的有效日期存在交叉,請重新選定日期！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                            Me.DateTimePicker2.Focus()
                            SendKeys.Send("{F4}")
                            Return False
                            Exit Function
                        ElseIf CDate(FormatDate(Me.DateTimePicker2.Value)) >= CDate(mRow("StartDate")) And CDate(FormatDate(Me.DateTimePicker2.Value)) <= CDate(mRow("EndDate")) Then
                            MsgBox("參數的有效日期存在交叉,請重新選定日期！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                            Me.DateTimePicker2.Focus()
                            SendKeys.Send("{F4}")
                            Return False
                            Exit Function
                        ElseIf CDate(FormatDate(Me.DateTimePicker1.Value)) = CDate(mRow("StartDate")) And CDate(FormatDate(Me.DateTimePicker2.Value)) = CDate(mRow("EndDate")) Then
                            MsgBox("存在相同的日期設定,請重新選定日期！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                            Me.DateTimePicker2.Focus()
                            SendKeys.Send("{F4}")
                            Return False
                            Exit Function
                        ElseIf CDate(FormatDate(Me.DateTimePicker1.Value)) <= CDate(mRow("StartDate")) And CDate(FormatDate(Me.DateTimePicker2.Value)) > CDate(mRow("EndDate")) Then
                            MsgBox("存在相同的日期設定,請重新選定日期！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                            Me.DateTimePicker2.Focus()
                            SendKeys.Send("{F4}")
                            Return False
                            Exit Function
                        End If
                    Next
                End If
            Catch ex As Exception

            Finally
                mTable.Dispose()
            End Try

        End If

        Dim IsExistRecord As String = ""
        sql = "select Count(*) from dbo.Logic_ParameterSetA where fCode='" & Me.txtCode.Text.Trim & "'"
        IsExistRecord = GetValue(sql)
        If IsExistRecord = "1" Then
            MsgBox(My.Resources.ExisedtSameCode, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Me.txtCode.Focus()
            Return False
        End If

        Return True
    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        BtnActionSave()
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect
        btnFind_Click(Nothing, Nothing)
    End Function



#End Region


#Region "sub or function"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ClearControl()
        Me.txtCode.Text = ""
        Me.txtRemark.Text = ""
        Me.cmbStore.SelectedIndex = -1
        Me.txtTon.Text = ""
        Me.txtSqure.Text = ""
        Me.txtMoney.Text = ""
        Me.TextBox4.Text = ""
        Me.TextBox5.Text = ""
        Me.ListView1.Items.Clear()
        Me.ListView2.Items.Clear()
        Me.CheckBox1.Checked = False
        Me.CheckBox2.Checked = False
        Me.CheckBox3.Checked = False
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="mBool"></param>
    ''' <remarks></remarks>
    Private Sub SetControlEnable(ByVal mBool As Boolean, ByVal B As Boolean)
        Me.txtCode.Enabled = B
        Me.txtRemark.Enabled = B
        Me.cmbStore.Enabled = B
        Me.DateTimePicker1.Enabled = B
        Me.DateTimePicker2.Enabled = B
        Me.CheckBox1.Enabled = B
        Me.CheckBox2.Enabled = B
        Me.CheckBox3.Enabled = B
        Me.TextBox3.Enabled = mBool
        Me.txtTon.Enabled = mBool
        Me.txtSqure.Enabled = mBool
        Me.txtMoney.Enabled = mBool
        Me.TextBox2.Enabled = mBool
        Me.TextBox4.Enabled = mBool
        Me.TextBox5.Enabled = mBool
        Me.Button1.Enabled = mBool
        Me.btnAdd.Enabled = mBool
        Me.btnEdit.Enabled = mBool
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BindGridView()

        Dim mTable As New DataTable
        Try

            With mTable.Columns
                .Add("fCode")
                .Add("StoreCode")
                .Add("StartDate")
                .Add("EndDate")
                .Add("Remark")
                .Add("CreateDate")
                .Add("CreatePerson")
                .Add("CreateComputer")
            End With

            Me.DataGridView1.DataSource = mTable

            With Me.DataGridView1
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = BindHash().Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            End With
        Catch ex As Exception
            MsgBox("BindGridView:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable.Dispose()
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function BindHash() As Hashtable

        Dim mHash As New Hashtable
        With mHash
            .Add("fCode", "編號")
            .Add("StoreCode", "倉庫")
            .Add("StartDate", "開始日期")
            .Add("EndDate", "結束日期")
            .Add("Remark", "備注")
            .Add("CreateDate", "建立日期")
            .Add("CreatePerson", "建立者")
            .Add("CreateComputer", "建立機器")
        End With

        Return mHash

    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadStore()

        Dim sql As String = ""
        Dim mTable As New DataTable
        Try
            sql = "select fCode,fShortName from dbo.BSC_StoreHouse"

            mTable = ReturnDataTable(sql)

            If mTable.Rows.Count > 0 Then
                Me.cmbStore.DisplayMember = "fShortName"
                Me.cmbStore.ValueMember = "fCode"
                Me.cmbStore.DataSource = mTable

                Dim nTable As DataTable = CType(Me.cmbStore.DataSource, DataTable)
                Me.cmbFindStore.DisplayMember = "fShortName"
                Me.cmbFindStore.ValueMember = "fCode"
                Me.cmbFindStore.DataSource = nTable

                nTable = Nothing
                mTable = Nothing
            End If
        Catch ex As Exception
            MsgBox("LoadStore:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function BtnActionSave() As Boolean

        If ActionInitialize() = False Then Exit Function
        Dim sqlConn As New SqlConnection
        Dim sqlcmd As New SqlCommand
        sqlConn = GetConn()

        If sqlConn.State = ConnectionState.Closed Then
            sqlConn.Open()
        End If
        Dim transqlserver As SqlTransaction = sqlConn.BeginTransaction
        Dim sql As String = ""
        Dim sqlstr As String = ""
        Dim StoreCode As String = ""
        Dim TransKg As String = ""
        Dim ServiceType As String = ""
        Try
           
            StoreCode = Me.cmbStore.SelectedValue.ToString

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then

                sql = "Insert into Logic_ParameterSetA(fCode, StoreCode,Remark, StartDate, EndDate, CreateDate, CreatePerson, CreateComputer) values('"
                sql += Me.txtCode.Text.Trim & "','" & StoreCode & "','" & Me.txtRemark.Text.Trim & "','" & FormatDate(Me.DateTimePicker1.Value) & "','" & FormatDate(Me.DateTimePicker2.Value) & "','" & ServerDate
                sql += "','" & PubUserCode & "','" & My.Computer.Name & "')"

                With sqlcmd
                    .Connection = sqlConn
                    .Transaction = transqlserver
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With
                sql = ""

                sql = "Insert into Logic_ParameterSetB(fCode,ServiceTag,ItemNo, ItemNoA, ItemNoB)" & vbCrLf
                If Me.CheckBox1.Checked = True Then
                    sqlstr += "select '" & Me.txtCode.Text & "','" & Me.cb1.Tag & "','01','" & Me.Label13.Text & "','" & Me.txtTon.Text.Trim & "'" & vbCrLf
                    sqlstr += "union all" & vbCrLf
                    sqlstr += "select '" & Me.txtCode.Text & "','" & Me.cb2.Tag & "','01','" & Me.Label15.Text & "','" & Me.txtSqure.Text.Trim & "'" & vbCrLf
                    sqlstr += "union all" & vbCrLf
                    If Me.ListView2.Items.Count > 0 Then
                        For i As Integer = 0 To Me.ListView2.Items.Count - 1
                            sqlstr += "select '" & Me.txtCode.Text.Trim & "','" & Me.cb3.Tag & "','" & Me.ListView2.Items(i).Text & "','" & Me.ListView2.Items(i).SubItems(1).Text & "','" & Me.ListView2.Items(i).SubItems(2).Text & "'" & vbCrLf
                            sqlstr += "union all" & vbCrLf
                        Next
                    End If
                End If
                If Me.CheckBox2.Checked = True Then
                    sqlstr += "select '" & Me.txtCode.Text & "','" & Me.CheckBox2.Tag & "','01','" & Me.Label9.Text & "','" & Me.txtMoney.Text & "'" & vbCrLf
                    sqlstr += "union all" & vbCrLf
                End If

                If Me.CheckBox3.Checked = True Then
                    If Me.ListView1.Items.Count > 0 Then
                        For i As Integer = 0 To Me.ListView1.Items.Count - 1
                            If i = Me.ListView1.Items.Count - 1 Then
                                sqlstr += "select '" & Me.txtCode.Text.Trim & "','" & Me.CheckBox3.Tag & "','" & Me.ListView1.Items(i).Text.Trim & "','" & Me.ListView1.Items(i).SubItems(1).Text & "','" & Me.ListView1.Items(i).SubItems(2).Text & "'" & vbCrLf
                            Else
                                sqlstr += "select '" & Me.txtCode.Text.Trim & "','" & Me.CheckBox3.Tag & "','" & Me.ListView1.Items(i).Text.Trim & "','" & Me.ListView1.Items(i).SubItems(1).Text & "','" & Me.ListView1.Items(i).SubItems(2).Text & "'" & vbCrLf
                                sqlstr += "union all" & vbCrLf
                            End If
                        Next
                    End If
                End If

                sql = sql + sqlstr
                With sqlcmd
                    .Connection = sqlConn
                    .Transaction = transqlserver
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With

                AddToDataSet(Me.txtCode.Text.Trim, Me.cmbStore.Text, FormatDate(Me.DateTimePicker1.Value), FormatDate(Me.DateTimePicker2.Value), Me.txtRemark.Text.Trim, ServerDate, PubUserCode, My.Computer.Name)
            Else

                sql = "update Logic_ParameterSetA set Remark='" & Me.txtRemark.Text.Trim & "',StoreCode='" & StoreCode & "',LastUpdateDate='" & ServerDate & "',LastUpdatePerson='"
                sql += PubUserCode & "',LastUpdateComputer='" & My.Computer.Name & "',StartDate='" & FormatDate(Me.DateTimePicker1.Value) & "',EndDate='" & FormatDate(Me.DateTimePicker2.Value) & "'"
                sql += " where fCode='" & Me.txtCode.Text.Trim & "';"
                sql += "Delete Logic_ParameterSetB where fCode='" & Me.txtCode.Text & "'"
                With sqlcmd
                    .Connection = sqlConn
                    .Transaction = transqlserver
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With

                sql = ""

                sql = "Insert into Logic_ParameterSetB(fCode,ItemNo, ItemNoA, ItemNoB) values('"
                If Me.CheckBox1.Checked = True Then
                    sqlstr += "select '" & Me.txtCode.Text & "','" & Me.cb1.Tag & "','01','" & Me.Label13.Text & "','" & Me.txtTon.Text.Trim & "'" & vbCrLf
                    sqlstr += "union all" & vbCrLf
                    sqlstr += "select '" & Me.txtCode.Text & "','" & Me.cb2.Tag & "','01','" & Me.Label15.Text & "','" & Me.txtSqure.Text.Trim & "'" & vbCrLf
                    sqlstr += "union all" & vbCrLf
                    If Me.ListView2.Items.Count > 0 Then
                        For i As Integer = 0 To Me.ListView2.Items.Count - 1
                            sqlstr += "select '" & Me.txtCode.Text.Trim & "','" & Me.cb3.Tag & "','" & Me.ListView2.Items(i).Text & "','" & Me.ListView2.Items(i).SubItems(1).Text & "','" & Me.ListView2.Items(i).SubItems(2).Text & "'" & vbCrLf
                            sqlstr += "union all" & vbCrLf
                        Next
                    End If
                End If
                If Me.CheckBox2.Checked = True Then
                    sqlstr += "select '" & Me.txtCode.Text & "','" & Me.CheckBox2.Tag & "','01','" & Me.Label19.Text & "','" & Me.txtMoney.Text & "'" & vbCrLf
                    sqlstr += "union all" & vbCrLf
                End If

                If Me.CheckBox3.Checked = True Then
                    If Me.ListView1.Items.Count > 0 Then
                        For i As Integer = 0 To Me.ListView1.Items.Count - 1
                            If i = Me.ListView1.Items.Count - 1 Then
                                sqlstr += "select '" & Me.txtCode.Text.Trim & "','" & Me.CheckBox3.Tag & "','" & Me.ListView1.Items(i).Text.Trim & "','" & Me.ListView1.Items(i).SubItems(1).Text & "','" & Me.ListView1.Items(i).SubItems(2).Text & "'" & vbCrLf
                            Else
                                sqlstr += "select '" & Me.txtCode.Text.Trim & "','" & Me.CheckBox3.Tag & "','" & Me.ListView1.Items(i).Text.Trim & "','" & Me.ListView1.Items(i).SubItems(1).Text & "','" & Me.ListView1.Items(i).SubItems(2).Text & "'" & vbCrLf
                                sqlstr += "union all" & vbCrLf
                            End If
                        Next
                    End If
                End If

                sql = sql + sqlstr
                With sqlcmd
                    .Connection = sqlConn
                    .Transaction = transqlserver
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With
                EditToDataSet(Me.txtCode.Text.Trim, Me.cmbStore.Text, FormatDate(Me.DateTimePicker1.Value), FormatDate(Me.DateTimePicker2.Value), Me.txtRemark.Text.Trim)
            End If

            transqlserver.Commit()
            sqlConn.Close()

            MsgBox(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.resources.Addsuccess, My.resources.EditSuccess), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)

            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            SetControlEnable(False, False)
            bFind = True
            MainForm.BtnEnable(bFind)

        Catch ex As Exception
            transqlserver.Rollback()
            MsgBox("BtnActionSave:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            sqlConn.Close()
            sqlcmd.Dispose()
            transqlserver.Dispose()
        End Try

    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="fCode"></param>
    ''' <param name="TransKg"></param>
    ''' <param name="TransKgPrice"></param>
    ''' <param name="TransKmKgPrice"></param>
    ''' <param name="TransKmSqurePrice"></param>
    ''' <param name="StoreDaySqurePrice"></param>
    ''' <param name="StartDate"></param>
    ''' <param name="EndDate"></param>
    ''' <remarks></remarks>
    Private Sub AddToDataSet(ByVal fCode As String, ByVal StoreCode As String, ByVal StartDate As String, ByVal EndDate As String, ByVal Remark As String, ByVal CreateDate As String, ByVal CreatePerson As String, ByVal CreateComputer As String)

        Dim mTable As DataTable = CType(Me.DataGridView1.DataSource, DataTable)
        Dim NewRow As DataRow = mTable.NewRow

        Try
            NewRow("fCode") = fCode
            NewRow("StoreCode") = StoreCode
            NewRow("StartDate") = StartDate
            NewRow("EndDate") = EndDate
            NewRow("Remark") = Remark
            NewRow("CreateDate") = CreateDate
            NewRow("CreatePerson") = CreatePerson
            NewRow("CreateComputer") = CreateComputer

            mTable.Rows.Add(NewRow)
            mTable.AcceptChanges()
            Me.DataGridView1.DataSource = mTable
        Catch ex As Exception
            MsgBox("AddToDataSet:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable.Dispose()
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub EditToDataSet(ByVal fCode As String, ByVal StoreCode As String, ByVal StartDate As String, ByVal EndDate As String, ByVal Remark As String)

        Dim mTable As DataTable = CType(Me.DataGridView1.DataSource, DataTable)
        Dim Rows() As DataRow = mTable.Select("fCode='" & fCode & "'")

        Try
            If Rows.Length > 0 Then
                Rows(0).Item("StoreCode") = StoreCode
                Rows(0).Item("StartDate") = StartDate
                Rows(0).Item("EndDate") = EndDate
                Rows(0).Item("Remark") = Remark
                mTable.AcceptChanges()

                Me.DataGridView1.DataSource = mTable
            End If
        Catch ex As Exception
            MsgBox("EditToDataSet:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable.Dispose()
        End Try


    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="fCode"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function DeleteDataset(ByVal fCode As String) As Boolean

        Dim sql As String = ""
        Dim mTable As DataTable = CType(Me.DataGridView1.DataSource, DataTable)
        Dim Rows() As DataRow = mTable.Select("fCode='" & fCode & "'")
        Try
            sql = "delete Logic_ParameterSetB where fCode='" & fCode & "';"
            sql += "delete Logic_ParameterSetA where fCode='" & fCode & "'"
            ReturnObject(sql)
            If Rows.Length > 0 Then
                mTable.Rows.Remove(Rows(0))
            End If
            mTable.Rows.RemoveAt(0)
            mTable.AcceptChanges()
            Me.DataGridView1.DataSource = mTable

            Return True
        Catch ex As Exception
            MsgBox("DeleteDataset:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable.Dispose()
        End Try

    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub FillText(ByVal fCode As String)

        Dim sql As String = ""
        Dim mTable As DataTable

        sql += "select fCode,ServiceTag,ItemNo,ItemNoA,ItemNoB from Logic_ParameterSetB where fCode='" & fCode & "'"
        mTable = New DataTable

        Try

            mTable = ReturnDataTable(sql)

            Me.txtCode.Text = Me.DataGridView1.CurrentRow.Cells("fCode").Value
            If Me.DataGridView1.CurrentRow.Cells("StoreCode").Value Is DBNull.Value Then
                Me.cmbStore.SelectedText = -1
            Else
                Me.cmbStore.Text = Me.DataGridView1.CurrentRow.Cells("StoreCode").Value
            End If
            Me.DateTimePicker1.Value = Me.DataGridView1.CurrentRow.Cells("StartDate").Value
            Me.DateTimePicker2.Value = Me.DataGridView1.CurrentRow.Cells("EndDate").Value
            Me.txtRemark.Text = Me.DataGridView1.CurrentRow.Cells("Remark").Value

            If mTable.Rows.Count > 0 Then

                Me.ListView1.Items.Clear()
                Me.ListView2.Items.Clear()

                For Each mRow As DataRow In mTable.Rows
                    Select Case mRow("ServiceTag").ToString.ToLower
                        Case Me.cb1.Tag.ToString.ToLower
                            Me.txtTon.Text = mRow("ItemNoB")
                        Case Me.cb2.Tag.ToString.ToLower
                            Me.txtSqure.Text = mRow("ItemNoB")
                        Case Me.cb3.Tag.ToString.ToLower
                            Dim Item As New ListViewItem
                            Item.Text = mRow("ItemNo")
                            Item.SubItems.Add(mRow("ItemNoA"))
                            Item.SubItems.Add(mRow("ItemNoB"))
                            Me.ListView2.Items.Add(Item)
                        Case Me.CheckBox2.Tag.ToString.ToLower
                            Me.txtMoney.Text = mRow("ItemNoB")
                        Case Me.CheckBox3.Tag.ToString.ToLower
                            Dim Item As New ListViewItem
                            Item.Text = mRow("ItemNo")
                            Item.SubItems.Add(mRow("ItemNoA"))
                            Item.SubItems.Add(mRow("ItemNoB"))
                            Me.ListView1.Items.Add(Item)
                    End Select
                Next

            End If

            bFind = mTable.Rows.Count > 0
        Catch ex As Exception
            MsgBox("FillText:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable.Dispose()
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetToolEnable()
        Try
            With MainForm
                .ToolStripAdd.Enabled = AddEnable
                .ToolStripEdit.Enabled = EditEnable And bFind = True
                .ToolStripDelete.Enabled = DeleteEnable And bFind = True
                .ToolStripPrint.Enabled = PrintEnable
                .ToolStripImport.Enabled = ImportEnable
                .ToolStripExport.Enabled = ExportEnable
                .ToolStripFind.Enabled = FindEnable
                .ToolStripFilter.Enabled = True
                .ToolStripCheck.Enabled = CheckEnable
                .ToolStripUnCheck.Enabled = UncheckEnable
                .ToolStripComplete.Enabled = CompleteEnable
                .AddToolStripMenuItem.Enabled = .ToolStripAdd.Enabled
                .Edit1ToolStripMenuItem.Enabled = .ToolStripEdit.Enabled
                .DelToolStripMenuItem.Enabled = .ToolStripDelete.Enabled
                .SaveToolStripMenuItem.Enabled = .ToolStripSave.Enabled
                .CancelToolStripMenuItem.Enabled = .ToolStripCancel.Enabled
                .PrintToolStripMenuItem.Enabled = .ToolStripPrint.Enabled
                .ExportToolStripMenuItem.Enabled = .ToolStripImport.Enabled
                .ImportToolStripMenuItem.Enabled = .ToolStripExport.Enabled
                .FilterToolStripMenuItem.Enabled = .ToolStripFilter.Enabled
            End With

        Catch ex As Exception
            MsgBox("GetToolEnable:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub



#End Region


#Region "Control Events"


    ''' <summary>
    ''' Load From
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Frm_ParameterSet_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try
            SetControlEnable(False, False)
            ClearControl()
            BindGridView()
            LoadStore()
            With Me.ListView1.Columns
                .Add("編號", 30, HorizontalAlignment.Left)
                .Add("項目名稱", 150, HorizontalAlignment.Left)
                .Add("單價", 40, HorizontalAlignment.Left)
            End With

            With Me.ListView2.Columns
                .Add("編號", 35, HorizontalAlignment.Left)
                .Add("噸位", 150, HorizontalAlignment.Left)
                .Add("單價", 70, HorizontalAlignment.Left)
            End With

            Me.cmbStore.DropDownStyle = ComboBoxStyle.DropDownList
        Catch ex As Exception

        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Me.TextBox3.Enabled = Me.CheckBox1.Checked
        Me.TextBox2.Enabled = Me.CheckBox1.Checked
        Me.txtTon.Enabled = Me.CheckBox1.Checked
        Me.txtSqure.Enabled = Me.CheckBox1.Checked
        Me.btnAdd.Enabled = Me.CheckBox1.Checked
        Me.btnEdit.Enabled = Me.CheckBox1.Checked
        Me.cb1.Checked = Me.CheckBox1.Checked
        Me.cb2.Checked = Me.CheckBox1.Checked
        Me.cb3.Checked = Me.CheckBox1.Checked
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub CheckBox2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        Me.txtMoney.Enabled = Me.CheckBox2.Checked
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub CheckBox3_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        Me.TextBox4.Enabled = Me.CheckBox3.Checked
        Me.TextBox5.Enabled = Me.CheckBox3.Checked
        Me.Button1.Enabled = Me.CheckBox3.Checked
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try

            Dim Item As New ListViewItem
            Item.Text = Format(Me.ListView1.Items.Count + 1, "00")
            Item.SubItems.Add(Me.TextBox4.Text.Trim)
            Item.SubItems.Add(Me.TextBox5.Text)
            If Me.ListView1.Items.Contains(Item) = False Then
                Me.ListView1.Items.Add(Item)
            End If
            Me.TextBox4.Text = ""
            Me.TextBox5.Text = ""
        Catch ex As Exception

        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click

        Try
            Dim Item As New ListViewItem
            Item.Text = Format(Me.ListView2.Items.Count + 1, "00")
            Item.SubItems.Add(Me.TextBox3.Text.Trim & "T")
            Item.SubItems.Add(Me.TextBox2.Text)
            For Each mItem As ListViewItem In Me.ListView2.Items

                If mItem.SubItems(1).Text.ToLower = Item.SubItems(1).Text.ToLower Then
                    MsgBox("存在相同的項目!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    Exit Sub
                End If
            Next

            Me.ListView2.Items.Add(Item)
            Me.TextBox3.Text = ""
            Me.TextBox2.Text = ""
        Catch ex As Exception
            MsgBox("btnAdd_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnEdit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEdit.Click

        Try
            Dim Item As New ListViewItem
            Item.Text = Me.TextBox3.Text.Trim
            Item.SubItems.Add(Me.TextBox2.Text)
            If Me.ListView1.Items.Contains(Item) = False Then
                Me.ListView1.Items.Add(Item)
            End If
            Me.TextBox3.Text = ""
            Me.TextBox2.Text = ""
        Catch ex As Exception
            MsgBox("btnAdd_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ListView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView1.KeyDown

        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then

            If e.KeyCode = Keys.Delete Then
                If MsgBox(My.resources.ConfirmDelete, MsgBoxStyle.Question + MsgBoxStyle.OkCancel, Msg.CompanyName) = MsgBoxResult.Ok Then
                    For Each Item As ListViewItem In Me.ListView1.Items
                        If Item.Selected = True Then
                            Item.Remove()
                        End If
                    Next
                    Me.ListView1.Refresh()
                End If
            End If

        End If


    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ListView2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ListView2.KeyDown

        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then

            If e.KeyCode = Keys.Delete Then
                If MsgBox(My.resources.ConfirmDelete, MsgBoxStyle.Question + MsgBoxStyle.OkCancel, Msg.CompanyName) = MsgBoxResult.Ok Then
                    For Each Item As ListViewItem In Me.ListView2.Items
                        If Item.Selected = True Then
                            Item.Remove()
                        End If
                    Next
                    Me.ListView2.Refresh()
                End If
            End If

        End If


    End Sub


    'Private Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    '    Try
    '        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
    '            If Me.ListView1.Items.Count > 0 Then
    '                Me.TextBox4.Text = 
    '                Me.TextBox5.Text = e.Item.SubItems(0).Text
    '            End If
    '        End If
    '    Catch ex As Exception

    '    End Try

    'End Sub

    'Private Sub ListView2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged

    '    Try
    '        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
    '            If Me.ListView1.Items.Count > 0 Then
    '                Me.TextBox4.Text = e.Item.Text
    '                Me.TextBox5.Text = e.Item.SubItems(0).Text
    '            End If
    '        End If
    '    Catch ex As Exception

    '    End Try

    'End Sub

    Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected

        Try
            If Not Me.TabControl1.SelectedTab Is Nothing Then
                Select Case Me.TabControl1.SelectedIndex
                    Case 0
                       
                    Case 1
                        If Me.DataGridView1.Rows.Count > 0 Then
                            Dim fCode As String = Me.DataGridView1.CurrentRow.Cells("fCode").Value
                            FillText(fCode)
                        End If
                End Select
            End If
        Catch ex As Exception
            MsgBox("TabControl1_Selected:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' '
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TabControl1_Selecting(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                e.Cancel = True
            End If
        Catch ex As Exception
            MsgBox("TabControl1_Selecting:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click

        Dim sql As String = ""
        Dim Sqlstr As String = ""
        Dim mTable As DataTable

        If Me.TextBox1.Text.Length > 0 Then
            Sqlstr += " and a.fCode like '%" & Me.txtCode.Text.Trim & "%'"
        End If

        If Me.cmbFindStore.SelectedIndex <> -1 Then
            Sqlstr += " and a.StoreCode='" & Me.cmbFindStore.SelectedValue.ToString & "'"
        End If

        Sqlstr += " and a.CreateDate between '" & FormatDate(Me.DateTimePicker3.Value) & "' and '" & FormatDate(Me.DateTimePicker4.Value) & "'"

        sql = "select a.fCode,b.fShortName as StoreCode,a.Remark,a.StartDate,a.EndDate,a.CreateDate,a.CreatePerson,a.CreateComputer from dbo.Logic_ParameterSetA a left join BSC_StoreHouse b on a.Storecode=b.fCode where 1=1" & Sqlstr
        mTable = New DataTable
        Try
            mTable = ReturnDataTable(sql)

            Me.DataGridView1.DataSource = mTable
            bFind = mTable.Rows.Count > 0
            GetToolEnable()
        Catch ex As Exception
            MsgBox("btnFind_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable.Dispose()
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataGridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.DoubleClick
        Me.TabControl1.SelectedIndex = 1
    End Sub

#End Region



    Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click

    End Sub
End Class
